import { Dropdown, Flex } from 'antd';
import {
  DeleteOutlined,
  EditOutlined,
  EyeOutlined,
  FileTextOutlined,
  ScissorOutlined
} from '@ant-design/icons';
import ActionUtil from '@/utils/action.js';
import ModalUtil from '@/utils/modal.js';
import { useUiStore } from '@/store/index.js';
import UiUtil from '@/utils/ui.js';
import { ResourceApi } from '@/api/resource/api.js';

/**
 * 文件右击菜单
 *
 * @param props 屬性
 * @param props.data 资源信息
 * @param props.children 子组件
 * @param props.onRename 重命名函数
 * @param props.onRefresh 刷新数据函数
 * @param props.onAttributes 属性函数
 */
export default function FileContextMenu(props) {
  const { data, onRename, onRefresh, onAttributes } = props;

  // 删除
  function deleted(data) {
    if (data?.resourceId == null) {
      ModalUtil.error('删除失败，ID不能为空');
      return;
    }
    ModalUtil.confirm(`确定要删除 ${data?.resourceName} 吗？`, () => {
      UiUtil.showLoading('删除中...');
      ResourceApi.deleteByIds([data?.resourceId])
        .then((res) => {
          const { code, msg } = res;
          if (code === 0) {
            ModalUtil.success(msg);
            onRefresh && onRefresh();
          } else {
            ModalUtil.error(msg);
          }
          UiUtil.hideLoading();
        })
        .catch(() => {
          UiUtil.hideLoading();
        });
    });
  }

  // 复制直链
  function copyLink(data) {
    ActionUtil.copyToClipboard(window.location.origin + `/v/${data?.encryptionId}`, () => {
      ModalUtil.success('复制成功');
    });
  }

  // 剪切文件
  function cutFileTo(data) {
    useUiStore.getState().setCutFile(data);
  }

  // 获取右击菜单
  function getContextMenu(data) {
    const items = [];
    if (data?.resourceType === 'file') {
      items.push({
        label: (
          <Flex gap={4} onClick={() => copyLink(data)}>
            <EyeOutlined />
            复制直链
          </Flex>
        ),
        key: '100'
      });
    }
    items.push(
      ...[
        {
          onClick: onAttributes,
          label: (
            <Flex gap={4}>
              <FileTextOutlined />
              属性
            </Flex>
          ),
          key: '300'
        },
        {
          onClick: () => {
            cutFileTo(data);
          },
          label: (
            <Flex gap={4}>
              <ScissorOutlined />
              剪切
            </Flex>
          ),
          key: '500'
        },
        {
          onClick: onRename,
          label: (
            <Flex gap={4}>
              <EditOutlined />
              重命名
            </Flex>
          ),
          key: '400'
        },
        {
          onClick: () => {
            deleted(data);
          },
          label: (
            <Flex gap={4} style={{ color: 'red' }}>
              <DeleteOutlined />
              删除
            </Flex>
          ),
          key: '200'
        }
      ]
    );
    return items;
  }

  return (
    <Dropdown
      menu={{
        items: getContextMenu(data)
      }}
      trigger={['contextMenu']}
    >
      <div style={{ height: '100%' }}>{props.children}</div>
    </Dropdown>
  );
}
